package xian.woniu.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xian.woniu.entity.Orders;
import xian.woniu.entity.User;
import xian.woniu.service.IDoctorService;
import xian.woniu.service.IOrdersService;
import xian.woniu.service.IPatientService;
import xian.woniu.service.IUserService;
import xian.woniu.util.ResultVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author SongHang
 * @time 2022/06/29 14:19:27
 */
@RestController
@RequestMapping("admin")
public class OrdersController {
    @Autowired
    private IPatientService patientService;

    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IDoctorService iDoctorService;

    @Autowired
    private IUserService iUserService;


    @PostMapping("create")
    public ResultVO createOrder(@RequestBody Orders orders) {
        ordersService.createOrder(orders);
        return ResultVO.success("下单成功");
    }


    @GetMapping("getPatientByDid/{did}")
    public List<Orders> getPatientByDid(@PathVariable("did")Integer did){
        List<Orders> allPatientByDid = ordersService.getAllPatientByDid(did);
        if(allPatientByDid.size()==0||allPatientByDid==null){
            return null;
        }
        return allPatientByDid;
    }

    @PostMapping("delete")
    public ResultVO deleteOrder(@RequestBody String oid) {
        ordersService.deleteOrder(Integer.parseInt(oid));
        return ResultVO.success("删除成功");
    }

    @PostMapping("update")
    public ResultVO updateOrder(@RequestBody Orders order) {
        ordersService.updateOrder(order);
        return ResultVO.success("修改成功");
    }

    @GetMapping("findall")
    public ResultVO findAllOrders() {
        List<Orders> ordersList = ordersService.findAllOrders();
        return ResultVO.success(ordersList);
    }

    @GetMapping("findone")
    public ResultVO findOrderByOid(@RequestBody String oid) {
        Orders order = ordersService.findOrderByOid(Integer.parseInt(oid));
        return ResultVO.success(order);
    }


    //扩展的拿uid查个人信息
    @PostMapping("findoneByUid/{uid}")
    public ResultVO findoneByUid(@PathVariable(value = "uid") String uid) {
        ResultVO resultVO = patientService.selPatientByUid(uid);
        return ResultVO.success(resultVO);
    }


    @GetMapping("getallsignpatient")
    public ResultVO getAllSignPatient() {
        List<Orders> ordersList = ordersService.getAllSignPatient();
        return ResultVO.success(ordersList);
    }

    //扩展的查询user表里flag=4,为未审核的医生
    @PostMapping(value = "findAllWithText")
    public ResultVO findAllWithText(@RequestBody String flag) {
        ResultVO rv = iDoctorService.findAllWithText();
        ResultVO rv2 = iUserService.findByFlag(Integer.parseInt(flag));

        Object data = rv.getData();
        Object data2 = rv2.getData();

        List<Map> list = (List<Map>) data;      // Doctor
        List<Map> list2 = (List<Map>) data2;    // User

        List<Map> doctors = new ArrayList<>();

        for (Map map : list) {
            for (Map map2 : list2) {
                if (map.get("uid").equals(map2.get("uid"))) {
                    map.putAll(map2);
                    doctors.add(map);
                    break;
                }
            }
        }
        return ResultVO.success(doctors);
    }
    @GetMapping("getallorders")
    public ResultVO getAllOrders(){
        List<Orders> ordersList=ordersService.getAllOrders();
        return ResultVO.success(ordersList);
    }
    @PostMapping("findDoctorId/{pid}")
    public ResultVO findDoctorId(@PathVariable String pid) {
        List<Integer> id = ordersService.findDoctorId(Integer.valueOf(pid));
        return ResultVO.success("查询成功", id);
    }

    /**
     * @CreateBy: hzz
     * @Description:    通过医生id查询签约记录
     * @CreateTime: 2022/7/6 16:45
     * @Params:  * @Param: null
     * @Return:
     */
    @GetMapping("findByDid")
    ResultVO findByDid(@RequestParam("did") Integer did){
        List<Orders> byDid = ordersService.findByDid(did);
        return ResultVO.success(byDid);
    }


    @PostMapping("updatePatientAndUser")
    public ResultVO updateFlag(@RequestBody User user) {
        User user1 = new User();
        user1.setUid(user.getUid());
        user1.setFlag(user.getFlag());
        ResultVO rv2 = iUserService.updateFlag(user1);
        return ResultVO.success("解聘成功");
    }
}